package com.order.controller.sys;

import java.util.ArrayList;
import java.util.List;
import com.order.config.Consts;
import com.jfinal.ext.kit.KeyLabel;
import com.jfinal.ext.render.chart.funshion.FunshionChartsRender;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.order.controller.BaseController;
import com.order.model.OrderLine;

/**主程序框架主页
 *  */
@ControllerBind(controllerKey = Consts.SYS + "welcome")
public class WelcomeController extends BaseController {

	protected final Logger logger = Logger.getLogger(getClass());
	List<KeyLabel> pies = new ArrayList<KeyLabel>();
	FunshionChartsRender fr = new FunshionChartsRender();
	
	/** 欢迎主页*/
	public void index() {
		render(Consts.SYS + "welcome.htm");
	}

	/**生成前七天 的订单量*/
	public void weekFollow() {		
		String sql =""
					+ " select count(*) total, date_sub(curdate(), interval ? day) date "
					+ " from order_head h, order_line l "
					+ " where "
					+ "		l.order_head_id = h.id "
					+ "		and datediff(h.create_date, "
					+ "					date_sub(curdate(), interval ? day)) = 0 ";
		for(int i = 1; i < 7; i++){	
			Record record = Db.findFirst(sql, 6-i, 6-i);
			System.out.println(record);
			String key = record.getNumber("total").toString();
			String createDate = record.getDate("date").toString();
			String label = createDate;//.substring(2, 8);
			KeyLabel e = new KeyLabel(key,label);
			pies.add(e);
		}
		fr.setPies(pies);
		fr.setHeight("300");
		fr.setWidth("500");
		fr.setCaption("最近7天订单 数");
		fr.setFlashFile("Column3D.swf");
		render(fr);
	}
	
	public void test() {
		OrderLine order = OrderLine.dao.findById(2);
		System.out.println(order);
		order.set(OrderLine.QUANTITY, "QUANTITY+1");
		System.out.println(order);
		order.update();
	}
}
